package com.gang.controller;

import com.gang.pojo.CommonResult;
import com.gang.service.DataService;
import com.gang.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    DataService dataService;

    @RequestMapping("/upload")
    @ResponseBody
    public List uploadFile(@RequestParam("file") MultipartFile uploadfile){
        File file = null;
        ArrayList<String> list = new ArrayList<>();
        try {
            file = FileUtil.MultipartFileToFile(uploadfile);
            String name = file.getName();
            name=name.substring(name.indexOf('.')+1,name.length());
            list.add(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
           list.add(IPFSUtil.upload(file.getName()));
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            IPFSUti.upload(file.getPath()+file.getName());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return list;
    }

    @RequestMapping("/imgwater")
    public void imgWater(@RequestParam("file") MultipartFile uploadfile,@RequestParam String waterinfo,HttpServletResponse response){
        File file = null;
        try {
            file = FileUtil.MultipartFileToFile(uploadfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
       ImgWaterUtil.mark(file, Color.WHITE, waterinfo);
        String fileName=waterinfo+".png";
        // 如果文件名不为空，则进行下载
        if (fileName != null) {
            //设置文件路径
            String realPath = "H:/";
            File file1 = new File(realPath, fileName);

            // 如果文件名存在，则进行下载
            if (file1.exists()) {

                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                try {
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                response.setHeader("Content-Length",""+file1.length());
                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file1);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }


    }

    @RequestMapping("/wordwater")
    public void wordWater(@RequestParam("file") MultipartFile uploadfile, @RequestParam String waterinfo,HttpServletResponse response){
        File file = null;
        try {
            file = FileUtil.MultipartFileToFile(uploadfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {

            String path = file.getAbsolutePath();
           String topath="H:\\"+waterinfo+".doc";
            WordWaterUtil.water(path,topath, "#"+waterinfo);
            String pdfpath=topath.replaceAll("doc","pdf");
            GetPDF.get(topath,pdfpath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String fileName=waterinfo+".pdf";
        if (fileName != null) {
            //设置文件路径
            String realPath = "H:/";
            File file1 = new File(realPath, fileName);

            // 如果文件名存在，则进行下载
            if (file1.exists()) {

                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                try {
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                response.setHeader("Content-Length",""+file1.length());
                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file1);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    @RequestMapping("/deletewater")
    @ResponseBody
    public CommonResult DeleteWater(@RequestParam("file") MultipartFile uploadfile){
        File file = null;
        String s=null;
        try {
            file = FileUtil.MultipartFileToFile(uploadfile);
             s = GetWater.get(file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult(200,"water",s);

    }

    @RequestMapping("/imgdownload/{waterinfo}")
    @ResponseBody
    public int downloadimg( HttpServletResponse response,@PathVariable("waterinfo") String waterinfo) throws UnsupportedEncodingException {

        String fileName = waterinfo+".png"; //下载的文件名

        // 如果文件名不为空，则进行下载
        if (fileName != null) {
            //设置文件路径
            String realPath = "H:/";
            File file = new File(realPath, fileName);

            // 如果文件名存在，则进行下载
            if (file.exists()) {

                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                response.setHeader("Content-Length",""+file.length());
                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return 1;
    }

    @RequestMapping("/pdfdownload/{waterinfo}")
    @ResponseBody
    public int downloadpdf( HttpServletResponse response,@PathVariable("waterinfo") String waterinfo) throws UnsupportedEncodingException {

        String fileName = waterinfo+".pdf"; //下载的文件名

        // 如果文件名不为空，则进行下载
        if (fileName != null) {
            //设置文件路径
            String realPath = "H:/";
            File file = new File(realPath, fileName);

            // 如果文件名存在，则进行下载
            if (file.exists()) {

                // 配置文件下载
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                // 下载文件能正常显示中文
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                response.setHeader("Content-Length",""+file.length());
                // 实现文件下载
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return 1;
    }
}
